//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CMSMMDriftCalculator.h"
using namespace Cephei::QL::Models::Marketmodels::Driftcomputation;
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Models/Marketmodels/Curvestates/CMSwapCurveState.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Models::Marketmodels::Curvestates;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Driftcomputation::CCMSMMDriftCalculator::CCMSMMDriftCalculator (Cephei::QL::Math::IMatrix^ pseudo, Cephei::Core::IVector<Double>^ displacements, Cephei::Core::IVector<Double>^ taus, UInt64 numeraire, UInt64 alive, UInt64 spanningFwds) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CMatrix^ _Cpseudo;
    try
    {
#ifdef HANDLE
        _phCMSMMDriftCalculator = NULL;
#endif
        _Cpseudo = safe_cast<CMatrix^> (pseudo);
        _Cpseudo->Lock();
        QuantLib::Matrix& _pseudo = static_cast<QuantLib::Matrix&> (_Cpseudo->GetReference ()); 
        displacements->Lock();
        INativeVector<Double>^ _NCIdisplacements = displacements->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdisplacements = safe_cast<CDoubleVector^>(_NCIdisplacements);
        std::vector<QuantLib::Spread>& _displacements = static_cast<std::vector<QuantLib::Spread>&> (_NCdisplacements->GetReference ());
        taus->Lock();
        INativeVector<Double>^ _NCItaus = taus->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCtaus = safe_cast<CDoubleVector^>(_NCItaus);
        std::vector<QuantLib::Time>& _taus = static_cast<std::vector<QuantLib::Time>&> (_NCtaus->GetReference ());
        QuantLib::Size _numeraire = (QuantLib::Size)ValueHelper::Convert (numeraire); //d
        QuantLib::Size _alive = (QuantLib::Size)ValueHelper::Convert (alive); //d
        QuantLib::Size _spanningFwds = (QuantLib::Size)ValueHelper::Convert (spanningFwds); //d
        _ppCMSMMDriftCalculator = new boost::shared_ptr<QuantLib::CMSMMDriftCalculator> (new QuantLib::CMSMMDriftCalculator ( _pseudo,  _displacements,  _taus,  _numeraire,  _alive,  _spanningFwds ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpseudo != nullptr) _Cpseudo->Unlock();
        if (displacements != nullptr) displacements->Unlock();    //not optional
        if (taus != nullptr) taus->Unlock();    //not optional
    }
}
Cephei::QL::Models::Marketmodels::Driftcomputation::CCMSMMDriftCalculator::CCMSMMDriftCalculator (boost::shared_ptr<QuantLib::CMSMMDriftCalculator>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCMSMMDriftCalculator = NULL;
#endif
	_ppCMSMMDriftCalculator = &childNative;
    
}
Cephei::QL::Models::Marketmodels::Driftcomputation::CCMSMMDriftCalculator::CCMSMMDriftCalculator (QuantLib::CMSMMDriftCalculator& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phCMSMMDriftCalculator = NULL;
#endif
	_ppCMSMMDriftCalculator = new boost::shared_ptr<QuantLib::CMSMMDriftCalculator> (&childNative);
    
    _CMSMMDriftCalculatorOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::Driftcomputation::CCMSMMDriftCalculator::CCMSMMDriftCalculator (CCMSMMDriftCalculator^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCMSMMDriftCalculator = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCMSMMDriftCalculator = new boost::shared_ptr<QuantLib::CMSMMDriftCalculator> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::Driftcomputation::CCMSMMDriftCalculator::CCMSMMDriftCalculator (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCMSMMDriftCalculator = NULL;
#endif
	if (!t->IsSubclassOf(CCMSMMDriftCalculator::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Driftcomputation::CCMSMMDriftCalculator::CCMSMMDriftCalculator (QuantLib::Handle<QuantLib::CMSMMDriftCalculator>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCMSMMDriftCalculator = &childNative;
	_ppCMSMMDriftCalculator = &static_cast<boost::shared_ptr<QuantLib::CMSMMDriftCalculator>>(childNative.currentLink());
    
    _CMSMMDriftCalculatorOwner = owner;
}
Cephei::QL::Models::Marketmodels::Driftcomputation::CCMSMMDriftCalculator::CCMSMMDriftCalculator (QuantLib::Handle<QuantLib::CMSMMDriftCalculator> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCMSMMDriftCalculator = &childNative;
	_ppCMSMMDriftCalculator = &static_cast<boost::shared_ptr<QuantLib::CMSMMDriftCalculator>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Driftcomputation::CCMSMMDriftCalculator::CCMSMMDriftCalculator (QuantLib::CMSMMDriftCalculator childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCMSMMDriftCalculator = NULL;
#endif
	_ppCMSMMDriftCalculator = new boost::shared_ptr<QuantLib::CMSMMDriftCalculator> (new QuantLib::CMSMMDriftCalculator (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::Driftcomputation::CCMSMMDriftCalculator::~CCMSMMDriftCalculator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppCMSMMDriftCalculator != NULL)
    {
	    delete _ppCMSMMDriftCalculator;
        _ppCMSMMDriftCalculator = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Driftcomputation::CCMSMMDriftCalculator::!CCMSMMDriftCalculator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppCMSMMDriftCalculator != NULL)
    {
	    delete _ppCMSMMDriftCalculator;
    }
}
QuantLib::CMSMMDriftCalculator& Cephei::QL::Models::Marketmodels::Driftcomputation::CCMSMMDriftCalculator::GetReference ()
{
    if (_ppCMSMMDriftCalculator == NULL) throw REFNEW NativeNullException ();
	return **_ppCMSMMDriftCalculator;
}
boost::shared_ptr<QuantLib::CMSMMDriftCalculator>& Cephei::QL::Models::Marketmodels::Driftcomputation::CCMSMMDriftCalculator::GetShared ()
{
    if (_ppCMSMMDriftCalculator == NULL) throw REFNEW NativeNullException ();
	return *_ppCMSMMDriftCalculator;
}
QuantLib::CMSMMDriftCalculator* Cephei::QL::Models::Marketmodels::Driftcomputation::CCMSMMDriftCalculator::GetPointer ()
{
    if (_ppCMSMMDriftCalculator == NULL) throw REFNEW NativeNullException ();
	return &**_ppCMSMMDriftCalculator;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CMSMMDriftCalculator>& Cephei::QL::Models::Marketmodels::Driftcomputation::CCMSMMDriftCalculator::GetHandle ()
{
	if (_phCMSMMDriftCalculator == NULL)
	{
		_phCMSMMDriftCalculator = new Handle<QuantLib::CMSMMDriftCalculator> (*_ppCMSMMDriftCalculator);
	}
	return *_phCMSMMDriftCalculator;
}
#endif
bool Cephei::QL::Models::Marketmodels::Driftcomputation::CCMSMMDriftCalculator::HasNative () 
{
	return (_ppCMSMMDriftCalculator != NULL);
}

Cephei::QL::Models::Marketmodels::Driftcomputation::ICMSMMDriftCalculator^ Cephei::QL::Models::Marketmodels::Driftcomputation::CCMSMMDriftCalculator::Compute (Cephei::QL::Models::Marketmodels::Curvestates::ICMSwapCurveState^ cs, Cephei::Core::IVector<Double>^ drifts)
{
    CCMSwapCurveState^ _Ccs;
    try
    {
        _Ccs = safe_cast<CCMSwapCurveState^> (cs);
        _Ccs->Lock();
        QuantLib::CMSwapCurveState& _cs = static_cast<QuantLib::CMSwapCurveState&> (_Ccs->GetReference ()); 
        drifts->Lock();
        INativeVector<Double>^ _NCIdrifts = drifts->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdrifts = safe_cast<CDoubleVector^>(_NCIdrifts);
        std::vector<QuantLib::Real>& _drifts = static_cast<std::vector<QuantLib::Real>&> (_NCdrifts->GetReference ());
    	(*_ppCMSMMDriftCalculator)->compute ( _cs,  _drifts );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccs != nullptr) _Ccs->Unlock();
        if (drifts != nullptr) drifts->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Driftcomputation::ICMSMMDriftCalculator^ Cephei::QL::Models::Marketmodels::Driftcomputation::CCMSMMDriftCalculator_Factory::Create (Cephei::QL::Math::IMatrix^ pseudo, Cephei::Core::IVector<Double>^ displacements, Cephei::Core::IVector<Double>^ taus, UInt64 numeraire, UInt64 alive, UInt64 spanningFwds)
{
    return REFNEW CCMSMMDriftCalculator ( pseudo,  displacements,  taus,  numeraire,  alive,  spanningFwds);
}
